<template>
	<view>
		<view class="add">
			<input type="text" v-model="id" placeholder="请输入id">
			<input type="text" v-model="addData.username" placeholder="请输入姓名">
			<input type="text" v-model="addData.age" placeholder="请输入年龄">
			<input type="text" v-model="addData.sex" placeholder="请输入性别">
			<button @click="add">添加</button>
			<button @click="modify">修改</button>
		</view>
		<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}"  collection="list">
			<view v-if="error">{{error.message}}</view>
			<view v-else>
				<view class="list-item" v-for="(v, i) in data" :key="i">
					姓名：{{ v.username }}--年龄： {{ v.age  }}--性别： {{ v.sex }}
					-- <button size="mini" @click="$refs.udb.remove(v._id)">删除</button>
				</view>
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	export default {
		methods: {
			add() {
				this.$refs.udb.add(this.addData, {
					success: () => {
						this.$refs.udb.refresh()
					}
				})
				
			},
			modify() {
				this.$refs.udb.update(this.id, this.addData, {
					success: () => {
						this.$refs.udb.refresh()
					}
				})
			}
		},
		data() {
			return {
				addData: {
					username: '',
					age: '',
					sex: ''
				},
				id: ""
			}
		}
	}
</script>

<style>
	.list-item {
		display: flex;
		align-items: center;
	}
</style>